package com.example.thornchg.yc_plane_game;

import com.example.thornchg.yc_plane_game.enemy.Enemy;
import com.example.thornchg.yc_plane_game.enemy.Enemy_1;
import com.example.thornchg.yc_plane_game.enemy.Enemy_2;
import com.example.thornchg.yc_plane_game.enemy.Enemy_3;

import org.cocos2d.actions.CCScheduler;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.types.CGSize;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class EnemyCreator {
    private List<Enemy> enemys;
    private CCNode target;
    private Random random;
    private CGSize winSize;
    private int step;

    public EnemyCreator(CCLayer target) {
        this.target = target;
        winSize = CCDirector.sharedDirector().getWinSize();
        random = new Random();
        enemys = new ArrayList<>();
    }

    public void createEnemy(float t) {
        int count = random.nextInt(2) + 1;
        for (int i = 0; i < count; i++) {
            Enemy ccSprite_enemy = null;
            for (Enemy enemy : enemys) {
                if (enemy instanceof Enemy_1&&!enemy.getVisible()) {
                    enemy.resume();
                    ccSprite_enemy = enemy;
                    break;
                }
            }
            if (ccSprite_enemy == null) {
                ccSprite_enemy = new Enemy_1();
                enemys.add(ccSprite_enemy);
                target.addChild(ccSprite_enemy);
            }
        }
        if (step%5==0){
            Enemy ccSprite_enemy = null;
            for (Enemy enemy : enemys) {
                if (enemy instanceof Enemy_2&&!enemy.getVisible()) {
                    enemy.resume();
                    ccSprite_enemy = enemy;
                    break;
                }
            }
            if (ccSprite_enemy == null) {
                ccSprite_enemy = new Enemy_2();
                enemys.add(ccSprite_enemy);
                target.addChild(ccSprite_enemy);
            }
        }
        if (step%15==0){
            Enemy ccSprite_enemy = null;
            for (Enemy enemy : enemys) {
                if (enemy instanceof Enemy_3 &&!enemy.getVisible()) {
                    enemy.resume();
                    ccSprite_enemy = enemy;
                    break;
                }
            }
            if (ccSprite_enemy == null) {
                ccSprite_enemy = new Enemy_3();
                enemys.add(ccSprite_enemy);
                target.addChild(ccSprite_enemy);
            }
            step=1;
        }
        step++;
    }

    public void start() {
        CCScheduler.sharedScheduler().schedule("createEnemy", this,
                .5f, false);
        step=0;
    }

    public void stop() {
        CCScheduler.sharedScheduler().unschedule("createEnemy", this);
        for (Enemy enemy : enemys) {
            enemy.setVisible(false);
            enemy.setIsDead(true);
            enemy.stopAllActions();
        }
    }

    public List<Enemy> getEnemys_1() {
        return enemys;
    }
}
